//将parent环境set到自己的环境中
window.electron = window.top.electron
window.$ = (args) => window.top.$(args, document);
document.notification = window.top.document.notification
document.print = window.top.document.print
document.println = window.top.document.println
document.printclear = window.top.document.printclear
window.GetUUID = window.top.GetUUID
document.Page = window.top.document.Page

document.println("请先拖入npk文件或输入npk文件路径,然后建立缓存。之后才可以使用搜索功能，以及为替他页面的pvf操作提供支持");

let keywordType = "#keyword"; //"#keyword" "#keywordMore"
const sql = electron.JDBC().sqlite;

const swtichKeywordTypeInit = (e) => {
    document.querySelector("#switchKeywordType").addEventListener("click", (e) => {
        console.log("swtichKeywordType")
        if (keywordType == "#keyword") {
            document.querySelector("#keyword").className = "hidden"
            document.querySelector("#keywordMore").className = ""
            keywordType = "#keywordMore"
        } else {
            document.querySelector("#keyword").className = ""
            document.querySelector("#keywordMore").className = "hidden"
            keywordType = "#keyword"
        }
    })
}

const newPvfHandle = (path) => {
    document.println("开始创建缓存...");

}

const pvfReplyInit = () => {
    electron.pvfReply((event, channel, token, err, data)=>{
        console.log(event, channel, token, err, data)
    })
}

//建立缓存
const buildCacheInit = () => {
    const btn = document.querySelector("#cache_btn")

    btn.addEventListener("click", (e) => {
        const input = document.querySelector("#pvfPath")
        if (!input.value || input.value == "") {
            document.println("请先选择pvf路径!")
            return;
        }

        btn.className = "wait";
        btn.disabled = true;

        //发送ipcRender channel
        electron.pvfSend("openPvfFile", "asda", input.value);
    });
}

//选择路径
const chooseBtnInit = () => {
    const chooseBtn = document.querySelector("#choose_btn");
    const input = document.querySelector("#pvfPath")

    chooseBtn.addEventListener("click", () => {
        chooseBtn.disabled = true

        //title, properties, filters
        electron.openFileDialog("选择文件", ["openFile"], [
            { name: 'pvf', extensions: ['pvf'] }
        ]).then(result => {
            console.log("result", result)
            chooseBtn.disabled = false

            //是否需取消
            if (result.canceled) {
                return;
            }

            //选择内容
            if (result.filePaths.length >= 1) {
                let path = result.filePaths[0];

                if (input.value == path) {
                    return;
                }

                input.value = path;
                newPvfHandle(path);
            }
        })
    });
}

const init = () => {

    //查询缓存
    sql.config.queryByName("pvf").then(result => {
        if (!result) {
            return;
        }

        const path = result.value;
        if (!path || path == "") {
            return;
        }

        const input = document.querySelector("#pvfPath")
        input.value = path;
        document.println("已加载缓存");
    })
}

//初始化拖拽文件事件
const dragFileInit = () => {
    const input = document.querySelector("#pvfPath")

    input.addEventListener("drop", (e) => {
        e.preventDefault()
        const files = e.dataTransfer.files;
        if (files && files.length > 0) {
            console.log(11)

            const path = files[0].path;//获取文件路径

            if (input.value == path) {
                return;
            }

            if (path.toLowerCase().indexOf(".pvf") == -1) {
                document.println("请拖入.pvf文件");
                input.className = "wrong"
                return;
            }

            // clearResult();
            input.className = ""
            input.value = path;
            newPvfHandle(path);
        }
    })

    //阻止拖拽结束事件默认行为
    input.addEventListener("dragover", (e) => {
        e.preventDefault();
    })
}

window.onload = () => {
    init();
    pvfReplyInit();
    dragFileInit();
    swtichKeywordTypeInit();
    chooseBtnInit();
    buildCacheInit();
}
